<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 */ 
namespace Mobile\Controller;
use Home\Logic\CartLogic;
use Think\Controller;
use Think\Page;
use Think\Verify;
class ArticleController extends MobileBaseController {
    public function index(){       
        $article_id = I('article_id',38);
    	$article = D('article')->where("article_id=$article_id")->find();
    	$this->assign('article',$article);
        $this->display();
    }
	
	//获取父分类信息，如果没有返回分类本身
	public function catinfo_parent($cat_id){
		$thiscate = M('ArticleCat')->where("cat_id = ".$cat_id."")->find();
		if($thiscate["parent_id"])
		{
			$parentcate = M('ArticleCat')->where("cat_id = ".$thiscate["parent_id"]."")->find();
		}
		else
		{
			$parentcate = $thiscate;
		}
		return $parentcate;
	}
	
    /**
     * 文章内列表页
     */
    public function articleList(){
		$cat_id = I('cat_id');
        $thiscate = M('ArticleCat')->where("cat_id = ".$cat_id."")->find();

		//查找是否有上级分类
		$parentcate = $this->catinfo_parent($cat_id);
		if($parentcate["cat_id"]==$thiscate["cat_id"])
		{
			$ifroot = 0;
		}
		else
		{
			$ifroot = 1;
		}
		$rootid = $parentcate["cat_id"];
		
		//子分类列表
        $article_soncat = M('ArticleCat')->where("parent_id  = ".$rootid."")->select();
		
		//文章列表
		$article_list = M('Article')->where("cat_id  = ".$cat_id." and is_open=1")->order("article_id DESC")->select();
		//截取文字
		foreach($article_list AS $key=>$value)
		{
			$value["cut_content"] = getSubstr(strip_tags(htmlspecialchars_decode($value["content"])),0,150);
			$article_list[$key] = $value;
		}

        $this->assign('cat_name','列表');
		$this->assign('cat_id',$cat_id);
		$this->assign('ifroot',$ifroot);
		$this->assign('thiscate',$thiscate);
		$this->assign('parentcate',$parentcate);
		$this->assign('article_soncat',$article_soncat);
		$this->assign('article_list',$article_list);
        $this->display();
    }    
    /**
     * 文章内容页
     */
    public function detail(){
    	$article_id = I('article_id',1);
    	$article = D('article')->where("article_id=$article_id")->find();
    	if($article['cat_id']==27){//常见问答拿来做关键词优化的设置头信息
            $this->assign('g_title',$article['g_title']);
            $this->assign('g_keywords',$article['g_keywords']);
            $this->assign('g_description',$article['g_description']);




//            $this->assign('g_title',$article['g_title']);
//            $this->assign('g_keywords',$article['g_keywords']);
//            $this->assign('g_description',$article['g_description']);
        }

    	if($article){
    		$parent = D('article_cat')->where("cat_id=".$article['cat_id'])->find();
			
			$cat_id = $article['cat_id'];
			$thiscate = M('ArticleCat')->where("cat_id = ".$cat_id."")->find();
			//查找是否有上级分类
			$parentcate = $this->catinfo_parent($cat_id);
			if($parentcate["cat_id"]==$thiscate["cat_id"])
			{
				$ifroot = 0;
			}
			else
			{
				$ifroot = 1;
			}
			$rootid = $parentcate["cat_id"];
			
			//子分类列表
	        $article_soncat = M('ArticleCat')->where("parent_id  = ".$rootid."")->select();
			
			//文章列表
			$article_list = M('Article')->where("cat_id  = ".$cat_id."")->order("sort ASC")->select();
			//手机版把购买流程id替换掉
			foreach($article_list AS $key=>$value)
			{
				if($value["article_id"]!=28 && $value["article_id"]!=29)
				{
					$artlist[] = $value;
				}
			}
			$article_list = $artlist;
			//截取文字
			foreach($article_list AS $key=>$value)
			{
				$value["cut_content"] = getSubstr(strip_tags(htmlspecialchars_decode($value["content"])),0,150);
				$article_list[$key] = $value;
			}
            $parent = D('article_cat')->where("cat_id=".$article['cat_id'])->find();

            $this->assign('cat_name',$parent['cat_name']);
			
			//随机获取业务员数据   （1条）
			$sales_where['status']=1;
			$sales_list=M('sales')->where($sales_where)->order('rand()')->limit(1)->select();
		 
			$this->assign('ifroot',$ifroot);
			$this->assign('thiscate',$thiscate);
			$this->assign('parentcate',$parentcate);
			$this->assign('article_soncat',$article_soncat);
			$this->assign('article_list',$article_list);
			$this->assign('article',$article);
			$this->assign('sales_list',$sales_list);
    	}
		 
		//自定义模板
		if($article["article_id"]==24)
		{
			$this->display("aboutus");
		}
		else
		{
	        $this->display();
		}
    }






    public function detailnews(){
    	$article_id = I('article_id',1);
    	$article = D('article')->where("article_id=$article_id")->find();
    	if($article){
    		$parent = D('article_cat')->where("cat_id=".$article['cat_id'])->find();

    		$this->assign('cat_name',$parent['cat_name']);
    		$this->assign('article',$article);;
    		$this->assign('article_id',$article_id);
    	}
        $this->display();
    }


    public function qa_list(){
		$id = I('id');
        I('type_id')?$where['type_id']=I('type_id'):$where['type_id']=1;
        $count = M('question_answer')->where($where)->count();
        $page = new Page($count,10);
        $show = $page->show();
        $list = M('question_answer')->where($where)->order("is_top ASC")->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('lists',$list);// 商品评论
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('type_id',$where['type_id']);
		$this->assign('id',$id);
        $this->display();
    }
}